package com.cfc.springcloud.controller;

import com.cfc.springcloud.entities.Dept;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class DeptController_Consumer {

    @Autowired
    private RestTemplate restTemplate;

    private final String PROVIDER_REST_PREFIX = "http://SPRINGCLOUD-DEPT";

    @RequestMapping(value="/consumer/dept/add",method = RequestMethod.POST)
    public Boolean add(Dept dept){
        return restTemplate.postForObject(PROVIDER_REST_PREFIX+"/dept/add",dept,Boolean.class);
    }

    @RequestMapping(value="/dept/get/{id}",method = RequestMethod.GET)
    public Dept getDept(@PathVariable("id") Long id){
        return restTemplate.getForObject(PROVIDER_REST_PREFIX+"/dept/get/"+id,Dept.class);
    }

    @RequestMapping(value="/dept/list",method = RequestMethod.GET)
    public List<Dept> list(){
        return restTemplate.getForObject(PROVIDER_REST_PREFIX+"/dept/list",List.class);
    }


    //测试@EnableDiscoveryClient,消费端可以调用服务发现
    @RequestMapping(value="/consumer/dept/discovery")
    public Object discovery()
    {
        return restTemplate.getForObject(PROVIDER_REST_PREFIX+"/dept/discovery", Object.class);
    }

}
